package com.example.monitor.controller;

import com.example.monitor.common.Result;
import com.example.monitor.exception.BusinessException;
import com.example.monitor.exception.NotFoundException;
import com.example.monitor.model.DTO.AlarmQueryDTO;
import com.example.monitor.model.DTO.AlarmRecordCreateDTO;
import com.example.monitor.model.DTO.AlarmStatusUpdateDTO;
import com.example.monitor.model.VO.AlarmRecordVO;
import com.example.monitor.model.VO.PageResult;
import com.example.monitor.model.enums.AlarmStatus;
import com.example.monitor.service.AlarmService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/alarm")
public class AlarmController {

    private final AlarmService alarmService;

    public AlarmController(AlarmService alarmService) {
        this.alarmService = alarmService;
    }

    @GetMapping("/records")
    public ResponseEntity<Result<PageResult<AlarmRecordVO>>> queryAlarmRecords(
            @RequestParam(required = false) String alarmType,
            @RequestParam(required = false) String workshop,
            @RequestParam(required = false) AlarmStatus status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        AlarmQueryDTO queryDTO = new AlarmQueryDTO();
        queryDTO.setAlarmType(alarmType);
        queryDTO.setWorkshop(workshop);
        queryDTO.setStatus(status);
        queryDTO.setStartTime(startTime);
        queryDTO.setEndTime(endTime);
        queryDTO.setPage(page);
        queryDTO.setPageSize(pageSize);

        PageResult<AlarmRecordVO> result = alarmService.queryAlarmRecords(queryDTO);
        return ResponseEntity.ok(Result.success(result));
    }

    @PutMapping("/record/{alarmId}")
    public ResponseEntity<Void> updateAlarmStatus(
            @PathVariable Long alarmId,
            @RequestBody AlarmStatusUpdateDTO updateDTO) {
        alarmService.updateAlarmStatus(alarmId, updateDTO);
        return ResponseEntity.ok().build();
    }

    // 新增报警记录
    @PostMapping("/record")
    public ResponseEntity<Result<Long>> addAlarmRecord(@RequestBody AlarmRecordCreateDTO alarmRecordCreateDTO) {
        try {
            Long alarmId = alarmService.addAlarmRecord(alarmRecordCreateDTO);

            // 手动创建结果，避免任何潜在的序列化问题
            Result<Long> result = Result.success(alarmId);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(Result.error(e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Result.error("系统异常"));
        }
    }

    // 删除报警记录
    @DeleteMapping("/record/{alarmId}")
    public ResponseEntity<Result<Void>> deleteAlarmRecord(@PathVariable Long alarmId) {
        try {
            alarmService.deleteAlarmRecord(alarmId);
            return ResponseEntity.ok(Result.success(null));
        } catch (NotFoundException e) {
            return ResponseEntity.notFound().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(Result.error(e.getMessage()));
        }
    }
}